// client/pages/menu/menu.js
var app = getApp();
var sliderWidth = 96; // 需要设置slider滑动条的宽度，用于计算中间位置
var foodList = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    foodNum: 0,
    sumprice: 0,
    psumprice: 0,
    mydata: [],
    myfoodList: [],
    // user: {
    //   sumPrice: sumprice,
    //   foodName: [],
    // },
    serveraddr: app.globalData.serveraddr
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function() {
    var that = this;
    // console.log(foodList);
    wx.request({
      url: app.globalData.serveraddr + '/menu',
      success: res => {
        // console.log(res.data)
        var gtList = []
        // 取数据
        for (var i = 0; i < res.data.gtlist.length; i++) {      //3次循环，每一次对应一个餐品类型
          gtList.push(res.data.gtlist[i].gt);       //把餐品类型的对象压入数组
        }
        this.setData({
          tabs: gtList,                 //数组里包含对象，但该对象仅包含餐品类型，用于小程序界面的“菜单栏横条”
          mydata: res.data.gtlist         //包含餐品完整信息的数组
        })
        wx.getSystemInfo({        //获取手机的硬件信息，便于数据更好的展示在手机上
          success: function (res) {
            that.setData({
              sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
              sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
            });
          }
        });
      }
    })
  },




  // 切换导航菜单栏（不换页面）
  tabClick: function(e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },

  // 购物加
  add: function(e) {
    var list = this.data.mydata;
    for (var i = 0; i < list.length; i++) {
      if (list[i].gt.gtid == e.currentTarget.dataset.gtid) {
        var glist = list[i].goodslist
        for (var j = 0; j < glist.length; j++) {
          if (glist[j].gid == e.currentTarget.dataset.gid) {
            glist[j].gcount += 1;
            var sumnum = this.data.foodNum + 1;
            var sum = this.data.sumprice + e.currentTarget.dataset.gprice;
            var psum = this.data.psumprice + e.currentTarget.dataset.gtime;
            this.setData({
              mydata: list,
              foodNum: sumnum,
              sumprice: sum,
              psumprice: sum,
            })
          }
        }
      }
    }
  },

  //购物减
  subtract: function(e) {
    var list = this.data.mydata;
    for (var i = 0; i < list.length; i++) {
      if (list[i].gt.gtid == e.currentTarget.dataset.gtid) {
        var glist = list[i].goodslist
        for (var j = 0; j < glist.length; j++) {
          if (glist[j].gid == e.currentTarget.dataset.gid) {
            if (glist[j].gcount > 0) {
              glist[j].gcount -= 1;
              var sumnum = this.data.foodNum - 1;
              var sum = this.data.sumprice - e.currentTarget.dataset.gprice;
              var psum = this.data.psumprice - e.currentTarget.dataset.gtime;
              this.setData({
                mydata: list,
                foodNum: sumnum,
                sumprice: sum,
                psumprice: psum,
              })
            }
          }
        }
      }
    }
  },

  //商品详细信息
  goinfo: function(e) {
    var list = this.data.mydata;        //餐品的完整信息
    for (var i = 0; i < list.length; i++) {       //3次
      if (list[i].gt.gtid == e.currentTarget.dataset.gtid) {        //先判断属于哪个类型
        var glist = list[i].goodslist
        for (var j = 0; j < glist.length; j++) {      //循环次数取决于每类餐品有多少个餐品实体
          if (glist[j].gid == e.currentTarget.dataset.gid) {        //再判断属于该类型的哪个餐品实体
            wx.navigateTo({
              url: '../info/info?foodinfo=' + JSON.stringify(glist[j])        //跳转，JSON.stringify()把对象转换成字符串。
            })
          }
        }
      }
    }
  },

 //支付
  pay: function() {
    var foodlist = new Array();
    for (var i = 0; i < this.data.mydata.length; i++) {
      for (var j = 0; j < this.data.mydata[i].goodslist.length; j++) {
        if (this.data.mydata[i].goodslist[j].gcount > 0) {        //存在购买的餐品
          foodlist.push(this.data.mydata[i].goodslist[j])         //把该餐品对象压入到购买数组之中
        }
      }
    };

    this.setData({
      myfoodList: foodlist
    })

    console.log(this.data.myfoodList);
    wx.navigateTo({
      url: '../usermenu/usermenu?foodslist=' + JSON.stringify(this.data.myfoodList)
    })
  },



  /**
 * 无用的生命周期函数
 */
lifefun:{
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
}



})